package entities 
{
	import org.flixel.*;
	import states.*;

	public class Disc extends FlxSprite 
	{
		[Embed(source = "../../art/red_disc.png")] private static var gfxRedDisc:Class;
		
		private var heightCounter:Number = 0;
		private var reachedPeak:Boolean = false;
		
		public function Disc() 
		{
			super( -200, -200, gfxRedDisc);
			
			offset.x = -8;
			offset.y = -24;
			
			antialiasing = true;
		}
		
		public function SendDisc(_x:Number, _y:Number, _xVel:Number, _yVel:Number):void {
			x = _x;
			y = _y;
			
			velocity.y = (_yVel * 2) + (FlxU.random() * 10);
			velocity.x = (_xVel * 2) + (FlxU.random() * 10);
			
			heightCounter = 1.1;
			reachedPeak = false;
		}
		
		override public function update():void {
			scale.x = heightCounter;
			scale.y = heightCounter;
			acceleration.x = 0;
			acceleration.y = 0;
			
			//scale.x = FlxU.max(scale.x, 1);
			//scale.y = FlxU.max(scale.y, 1);
			
			if (heightCounter >= 2) {
				reachedPeak = true;
			}
			
			if (!reachedPeak) {
				heightCounter += FlxG.elapsed * 3;
			}else {
				heightCounter -= FlxG.elapsed * 3;
			}
			
			acceleration.x = heightCounter;
			acceleration.y = heightCounter;
			//FlxU.overlap(this, PlayState.player, HitPlayer);
			
			if (heightCounter <= 1) {
				heightCounter = 1;
				velocity.y = 0;
				velocity.x = 0;
				acceleration.x = 0;
				acceleration.y = 0;
			}
			
			//if (x + 8 < PlayState.player.x + 9 && x + 8 > PlayState.player.x + 33 && y + 8 < PlayState.player.y + 20 && y + 8 > PlayState.player.y + 40) {
				//HitPlayer(PlayState.player, this);
			//}
			FlxU.overlap(PlayState.player, this, HitPlayer);
			
			angle += velocity.x + velocity.y;
			
			y = FlxU.max(-16, y);
			y = FlxU.min(FlxG.height - 28, y);
			x = FlxU.max(-16, x);
			x = FlxU.min(FlxG.width -232, x);
			
			super.update();
		}
		
		public function HitPlayer(p:FlxObject, disc:FlxObject):void {
			if (heightCounter == 1 && PlayState.player.discsHeld < 4 && !PlayState.player.pickup && !PlayState.player.shooting) {
				PlayState.player.pickup = true;
				x = -200;
				y = -200;
				velocity.y = 0;
				velocity.x = 0;
				heightCounter = 1;
				PlayState.player.discsHeld++;
				PlayState.player.play("pickup");
				PlayState.player.animationPlaying = true
			}
		}
		
	}
}